using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;


using System.Data.Linq;
using System.Linq;

using DataLayer;

namespace BusinessLayer.Activities
{
   public partial class Add_WidgetToTab : System.Workflow.ComponentModel.Activity
   {
      public Add_WidgetToTab()
      {
         //InitializeComponent();
      }

      private static DependencyProperty TabIdProperty = DependencyProperty.Register("TabId", typeof(int), typeof(Add_WidgetToTab));
      public int TabId
      {
         get { return (int)base.GetValue(TabIdProperty); }
         set { base.SetValue(TabIdProperty, value); }
      }

      private static DependencyProperty WidgetIdProperty = DependencyProperty.Register("WidgetId", typeof(int), typeof(Add_WidgetToTab));
      public int WidgetId
      {
         get { return (int)base.GetValue(WidgetIdProperty); }
         set { base.SetValue(WidgetIdProperty, value); }
      }

      private static DependencyProperty WidgetInstanceProperty = DependencyProperty.Register("WidgetInstance", typeof(TabWidget), typeof(Add_WidgetToTab));
      public TabWidget WidgetInstance
      {
         get { return (TabWidget)base.GetValue(WidgetInstanceProperty); }
         set { base.SetValue(WidgetInstanceProperty, value); }
      }

      private static DependencyProperty StateProperty = DependencyProperty.Register("State", typeof(string), typeof(Add_WidgetToTab));
      public string State
      {
         get { return (string)base.GetValue(StateProperty); }
         set { base.SetValue(StateProperty, value); }
      }

      private static DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(Add_WidgetToTab));
      public string Title
      {
         get { return (string)base.GetValue(TitleProperty); }
         set { base.SetValue(TitleProperty, value); }
      }

      private static DependencyProperty IconProperty = DependencyProperty.Register("Icon", typeof(string), typeof(Add_WidgetToTab));
      public string Icon
      {
         get { return (string)base.GetValue(IconProperty); }
         set { base.SetValue(IconProperty, value); }
      }
      
      protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
      {
         using (var db = DatabaseHelper.GetDashboardData())
         {
            var theWidget = db.Widgets.Single(p => p.ID == WidgetId);

            var obj = new TabWidget();
            obj.ColumnNo = 1;
            obj.CreatedDate = DateTime.Now;
            obj.Expanded = true;
            obj.LastUpdate = DateTime.Now;
            obj.OrderNo = 1;
            obj.State = (State.Length > 0 ? State : theWidget.DefaultState);
            obj.TabId = TabId;
            obj.Title = (Title != null && Title.Length > 0 ? Title : theWidget.Name);
            obj.WidgetId = WidgetId;
            obj.Icon = Icon;
            obj.Height = 0;

            DatabaseHelper.Insert<TabWidget>(obj);

            WidgetInstance = obj;

            //get all widgets in the first column and move them
            var WidgetsInFirstColumn = from p in db.TabWidgets
                                       where p.ColumnNo == 1 && p.TabId == TabId
                                       select p;
            foreach (var w in WidgetsInFirstColumn)
            {
               w.OrderNo = w.OrderNo + 1;
            }
            var e = new EntLib.EntTimedLog("", "AddWidgetToTabX");
            db.SubmitChanges();
            e.Dispose();
         }
        return ActivityExecutionStatus.Closed;
      }
   }
}